package com.panda_coder.liedetector;

import com.panda_coder.liedetector.utils.AsrUtils;
import ohos.ace.ability.AceAbility;
import ohos.aafwk.content.Intent;
import ohos.data.distributed.common.*;
import ohos.data.distributed.user.SingleKvStore;

import java.util.ArrayList;
import java.util.List;

public class MainAbility extends AceAbility {


    KvManagerConfig config;
    KvManager kvManager;
    private static final String STORE_ID = "distributed_db";
    private SingleKvStore singleKvStore;
    MKvStoreObserver mKvStoreObserver=new MKvStoreObserver();
    AsrUtils asrUtils;

    private SingleKvStore createDb() {
        SingleKvStore kvstore = null;
        try {
            Options options = new Options();
            options.setCreateIfMissing(true).setEncrypt(false).setKvStoreType(KvStoreType.SINGLE_VERSION);
            kvstore = kvManager.getKvStore(options, STORE_ID);
            asrUtils=new AsrUtils(this);
        } catch (KvStoreException exception) {

        }
        return kvstore;
    }


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        requestPermission();
        try{
            config = new KvManagerConfig(this.getContext());
            kvManager = KvManagerFactory.getInstance().createKvManager(config);
            singleKvStore = createDb();
            singleKvStore.subscribe(SubscribeType.SUBSCRIBE_TYPE_REMOTE,mKvStoreObserver);
        }catch (KvStoreException exception){

        }
    }

    //获取权限
    private void requestPermission() {
        String[] permission = {
                "ohos.permission.READ_USER_STORAGE",
                "ohos.permission.WRITE_USER_STORAGE",
                "ohos.permission.DISTRIBUTED_DATASYNC",
                "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO",
        };
        List<String> applyPermissions = new ArrayList<>();
        for (String element : permission) {
            if (verifySelfPermission(element) != 0) {
                if (canRequestPermission(element)) {
                    applyPermissions.add(element);
                }
            }
        }
        requestPermissionsFromUser(applyPermissions.toArray(new String[0]), 0);
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    class MKvStoreObserver implements KvStoreObserver{
        @Override
        public void  onChange(ChangeNotification notification){
            try{
                byte[] bytes= singleKvStore.getByteArray("PCM");
                asrUtils.start();
                asrUtils.writePCM(bytes);
               String result= asrUtils.getResult();
               if(result!=null){
                   try{
                     String old_text=  singleKvStore.getString("audio_text");
                     result+=old_text;
                   }catch (Exception ex){

                   }
                   singleKvStore.putString("audio_text",result);
               }
            }catch (Exception ex){

            }
        }
    }
}
